package cn.lijigou.shopping.service;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import cn.lijigou.shopping.SettingsActivity;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.Browser;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

public class PushListener extends BroadcastReceiver {
	private NotificationManager manager;
	private SharedPreferences share;
	@Override
	public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        share= context.getSharedPreferences("user", Context.MODE_PRIVATE);
        Boolean pushSwitch = share.getBoolean("push_switch", true);
         if(action.equals("cn.lookoo.push")){
        	 if(pushSwitch){
	         	ComponentName serviceName = new ComponentName(context, LookooPushService.class);
	        	Intent startintent = null;
	        	Log.i("pushSwitch", "-StartService-");
	        	startintent=new Intent("cn.lookoo.update.choice_shopping");
	        	startintent.setComponent(serviceName);
	        	context.startService(startintent);
        	 }else{
        		 cancelIntent(context);
        	 }
        }else {
        	
        	Log.i("pushSwitch", "--"+pushSwitch);
        	if(pushSwitch){
//        		if(getNetConnectState(context)){
//		        		Intent intetns=new Intent(context,PushListener.class);//
//		    			intetns.setAction("cn.lookoo.push");
//		    			Log.i("pendingIntent", alarmIsSet(intetns,context)+"-1--");
//		    			if(!alarmIsSet(intetns,context)){
//		    				PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intetns,0);
//			        		AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//			        		mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000*30,1000*30, pendingIntent);
//		    			} 
//        		}else{ 
//	        			cancelIntent(context);
//	        			Intent intetns=new Intent(context,PushListener.class);//
//		    			intetns.setAction("cn.lookoo.push");
//        		}
        	}else{
        		cancelIntent(context);
        	}
 
        } 
        
	}
	 private boolean alarmIsSet(Intent intent,Context context) {       
		 return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null;       
	 } 
	
	private void cancelIntent(Context context){
		Intent intetns=new Intent(context,PushListener.class);//
	    intetns.setAction("cn.lookoo.push");
	    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intetns, 0);
        AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
	    mAlarmManager.cancel(pendingIntent);
	    pendingIntent.cancel();
	    ComponentName serviceName = new ComponentName(context, LookooPushService.class);
    	Intent service=new Intent();
    	service.setComponent(serviceName);
		context.stopService(service);
		
		

	}
	
	public void showToast(String conten,Context context){
		Toast.makeText(context, conten, 1).show();
	}
	public boolean getNetConnectState(Context context) {
		boolean netConnect = false;
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo infoM = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (info.isConnected() || infoM.isConnected()) {
			netConnect = true;
		}
		return netConnect;
	}
	

	public boolean isServiceRunning(Context context,String className) {        
        boolean isRunning = false;
        ActivityManager activityManager = 
            (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE); 
        List<ActivityManager.RunningServiceInfo> serviceList 
        = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (!(serviceList.size()>0)) {
            return false;
        }
        for (int i=0; i<serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }

	
	
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
	
	
}
